package com.zjy.service.user;

import com.zjy.dao.BaseDao;
import com.zjy.dao.user.UserDao;
import com.zjy.dao.user.UserDaoImpl;
import com.zjy.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService{

    //业务层都会调用dao层，因此引入dao层
    private UserDao userDao;
    public UserServiceImpl(){
        userDao=new UserDaoImpl();
    }

    public User login(String userCode, String password) {
        Connection connection=null;
        User user=null;

        try {
            connection= BaseDao.getConnection();
            //通过业务层调用对应的具体的数据库操作
            user = userDao.getLoginUser(connection, userCode);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return user;
    }
//    @Test
//    public void test(){
//        UserServiceImpl userService = new UserServiceImpl();
//        //测试方法是否正确
//        User user = userService.login("admin", "1234555556");
//        System.out.println(user.getUserPassword());
//    }
    //根据用户ID修改用户密码
    public boolean updatePwd(int id, String password) {
        Connection connection=null;
        boolean flag=false;
        try {
            connection=BaseDao.getConnection();
            if(userDao.updatePwd(connection,id,password)>0){
                flag=true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }
    //查询记录数
    public int getUserCount(String username, int userRole) {
        Connection connection=null;
        int count=0;
        try {
            connection=BaseDao.getConnection();
            count=userDao.getUserCount(connection,username,userRole);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return count;
    }
    @Test
    public void test(){
        UserServiceImpl userService = new UserServiceImpl();
        int usercount = userService.getUserCount(null, 1);
        System.out.println(usercount);
    }

    //获取用户列表分页显示
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        Connection connection=null;
        List<User> userList=null;
        System.out.println("queryUserName----> "+queryUserName);
        System.out.println("queryUserRole----> "+queryUserRole);
        System.out.println("currentPageNo----> "+currentPageNo);

        try {
            connection=BaseDao.getConnection();
            userList=userDao.getUserList(connection,queryUserName,queryUserRole,currentPageNo,pageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return userList;
    }
    //添加用户
    public boolean add(User user) {
        boolean flag=false;
        Connection connection=null;
        try {
            connection=BaseDao.getConnection();
            //开启jdbc事务管理
            connection.setAutoCommit(false);
            int addRows=userDao.add(connection,user);
            connection.commit();
            if(addRows>0){
                flag=true;
                System.out.println("add success!");
            }else{
                System.out.println("add failed!");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                System.out.println("----rollback----");
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }
    //删除用户
    public boolean delUser(Integer delId) {
        Connection connection=null;
        boolean flag=false;
        try {
            connection=BaseDao.getConnection();
            if(userDao.delUser(connection,delId)>0){
                flag=true;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }


}
